/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: MapChooserScreen.java 603 2010-07-26 18:47:09Z Harry $
 *   $LastChangedDate: 2010-07-26 18:47:09 +0000 (Mon, 26 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.ui.screen;

import battlefield.resource.Graphics;
import battlefield.ui.component.MapChooserButton;
import battlefield.ui.component.NextButton;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 *
 * @author Harry Nguyen
 */
public class MapChooserScreen extends Screen implements ActionListener {

    JLabel mapSelectionLab;
    JLabel mapConfirmation;
    ButtonGroup mapGroup;
    MapChooserButton[] buttons;
    NextButton nextBtn;
    private String selectedMap;

    public MapChooserScreen() {
        initComponents();
    }

    public void initComponents() {
        try {
            mapGroup = new ButtonGroup();
            mapSelectionLab = new JLabel(new ImageIcon(Graphics.SCREEN_TITLE_MAP_SELECTION.getImage()));
            mapConfirmation = new JLabel(new ImageIcon(Graphics.MAP_CONFIRMATION.getImage()));
            mapConfirmation.setVisible(false);
            nextBtn = new NextButton();
            nextBtn.addActionListener(this);
            nextBtn.setVisible(false);
            String[] maps = getLogic().getMapNames();
            this.setPreferredSize(new Dimension(128 * maps.length, 128));
            buttons = new MapChooserButton[maps.length];
            for (int i = 0; i < maps.length; i++) {
                buttons[i] = new MapChooserButton(maps[i]);
                buttons[i].addActionListener(this);
                buttons[i].setPreferredSize(new Dimension(MapChooserButton.WIDTH, MapChooserButton.HEIGHT));
                buttons[i].setSize(new Dimension(MapChooserButton.WIDTH, MapChooserButton.HEIGHT));
                add(buttons[i]);
                mapGroup.add(buttons[i]);
            }
            this.setBackgroundImage(Graphics.BACKGROUND_MAIN.getImage());
            GroupLayout layout = new GroupLayout(this);
            this.setLayout(layout);
            layout.setAutoCreateGaps(true);
            layout.setAutoCreateContainerGaps(true);
            layout.setHorizontalGroup(
                    layout.createSequentialGroup().addGap(225, 225, 225).addGroup(layout.createParallelGroup().addComponent(mapSelectionLab).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(buttons[0], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[4], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(buttons[1], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[5], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(buttons[2], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[6], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(buttons[3], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[7], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))).addGroup(layout.createSequentialGroup().addComponent(mapConfirmation).addComponent(nextBtn))));
            layout.setVerticalGroup(
                    layout.createSequentialGroup().addComponent(mapSelectionLab).addGap(50, 50, 50).addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(buttons[0], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[1], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[2], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[3], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(buttons[4], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[5], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[6], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[7], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(mapConfirmation).addComponent(nextBtn)));
        } catch (RemoteException ex) {
            Logger.getLogger(MapChooserScreen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /* Begin Region: State Pattern */
    @Override
    public Screen waitForPlayers() {
        try {
            getLogic().createMap(getPlayer().getId(), selectedMap);
        } catch (RemoteException ex) {
        }
        return new WaitForPlayerScreen();
    }
    /* End Region: State Pattern */

    public String getChoosenMap() {
        for (int i = 0; i < buttons.length; i++) {
            if (buttons[i].isSelected()) {
                return buttons[i].getMapName();
            }
        }
        return null;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof MapChooserButton) {
            MapChooserButton button = (MapChooserButton) e.getSource();
            selectedMap = button.getMapName();
            mapConfirmation.setVisible(true);
            nextBtn.setVisible(true);
        } else if (e.getSource().equals(nextBtn)) {
            getGUI().waitForPlayers();
        }
    }
}
